<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7" class="hidden-xs-only">
      <h2>欢迎来到管理系统</h2>
      <el-image
        :src="require('@/assets/qq.png')"
        style="height: 350px;border-radius: 25px"
      ></el-image>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :xl="6" :lg="7">
      <el-card class="box-card" shadow="always">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginForm"
          label-width="65px"
          class="demo-loginForm"
        >
          <el-form-item label="用户名" prop="username" style="width: 330px;">
            <el-input v-model="loginForm.username" type="text"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 330px;">
            <el-input v-model="loginForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" style="width: 330px;">
            <el-input
              v-model="loginForm.code"
              type="text"
              style="width: 150px;float: left"
            ></el-input>
            <el-image
              :src="captchaImg"
              class="captchaImg"
              @click="getCaptcha"
            ></el-image>
          </el-form-item>
          <el-form-item style="width: 330px;">
            <el-button type="primary" @click="submitForm('loginForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import qs from 'qs'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        token: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' },
        ],
      },
      captchaImg: null,
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post('/login?' + qs.stringify(this.loginForm))
            .then((res) => {
              const jwt = res.headers['authorization']
              console.log(res)
              this.getUser()
              this.$store.commit('SET_TOKEN', jwt)
              //跳转页面
              this.$router.push('/index')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getCaptcha() {
      this.$axios.get('/captcha').then((res) => {
        console.log('/captcha')
        console.log(res)

        this.loginForm.token = res.data.data.token
        this.captchaImg = res.data.data.captchaImg
        this.loginForm.code = ''
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    getUser() {
      this.$axios.get('/sys/userInfo').then((res) => {
        this.userInfo = res.data.data
        this.$store.commit('SET_USER', this.userInfo)
      })
    },
  },
  created() {
    this.getCaptcha()
  },
}
</script>

<style scoped>
.el-row {
  display: flex;
  justify-items: center;
  justify-content: center;
  align-items: center;
  text-align: center;
  width: 100vw;
  height: 100vh;
  background: url('~@/assets/login.jpeg') center center no-repeat;
}
.el-card {
  width: 380px;
  border-radius: 25px;
  margin-left: 33px;
}
.el-divider {
  height: 200px;
}

.captchaImg {
  width: 105px;
  margin-left: 10px;
  float: left;
  border-radius: 4px;
}
</style>
